package hai.nguyenvan;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TableFrame extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private Connection conn;
	private String tableName;
	private DefaultTableModel model;
	
	
	public Connection getConn() {
		return conn;
	}


	public void setConn(Connection conn) {
		this.conn = conn;
	}


	public String getTableName() {
		return tableName;
	}


	public void setTableName(String tableName) {
		this.tableName = tableName;
	}


	public DefaultTableModel getModel() {
		return model;
	}


	public void setModel(DefaultTableModel model) {
		this.model = model;
	}

	private void loadData(){
		try {
			Statement stt = conn.createStatement();
			ResultSet rs = stt.executeQuery("Select * from " + getTableName());
			
			ResultSetMetaData metaData = rs.getMetaData();
			int numberColumn = metaData.getColumnCount();
			ArrayList<String> listColumn = new ArrayList<String>();
			for (int i = 1; i <= numberColumn ; i++) {
				listColumn.add(metaData.getColumnName(i));
			}
			
			model.setColumnIdentifiers(listColumn.toArray());
			ArrayList<String> listRow  = new ArrayList<String>();
			while(rs.next()){
				for (int i = 1; i <= numberColumn ; i++) {
					listRow.add(rs.getString(i));
				}
				model.addRow(listRow.toArray());
				listRow.clear();
			}
			table.setModel(model);
			rs.close();
			stt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * Create the frame.
	 */
	public TableFrame(String tableName, Connection conn) {
		this.tableName = tableName;
		this.conn = conn;
		model = new DefaultTableModel();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 599, 459);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 11, 563, 364);
		contentPane.add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		
		JButton btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int[] selectedRows = table.getSelectedColumns();
				for (int i = 0; i < selectedRows.length; i++) {
					model.removeRow(selectedRows[i]);
				}
			}
		});
		btnDelete.setBounds(484, 386, 89, 23);
		contentPane.add(btnDelete);
		
		JButton btnEdit = new JButton("Edit");
		btnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnEdit.setBounds(394, 386, 89, 23);
		contentPane.add(btnEdit);
		
		JButton btnInsert = new JButton("Insert");
		btnInsert.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				model.addRow(new Object[]{});
			}
		});
		btnInsert.setBounds(303, 386, 89, 23);
		contentPane.add(btnInsert);
//		contentPane.add(table);

		loadData();
	}
}
